package com.wx.tnsenterprise.controller;

import com.github.pagehelper.PageInfo;
import com.wx.common.api.CommonPage;
import com.wx.common.api.CommonResult;
import com.wx.tnsenterprise.model.dto.FunnelEntDto;
import com.wx.tnsenterprise.model.dto.PageDto;
import com.wx.tnsenterprise.model.po.EnterprisePo;
import com.wx.tnsenterprise.model.vo.EntDetailVo;
import com.wx.tnsenterprise.model.vo.EntHotVo;
import com.wx.tnsenterprise.model.vo.EntNavVo;
import com.wx.tnsenterprise.service.EnterpriseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 林建权
 * @version 1.0
 * @date 2020/7/28 17:11
 */

//企业信息接口
@CrossOrigin
@RestController
@RequestMapping("/enterprise")
@Api(tags = "企业信息接口")
public class EnterpriseController {
    @Autowired
    private EnterpriseService enterpriseService;

    //前端需求
    //首页热招
    @GetMapping("/findHotEnt")
    @ApiOperation(value = "查询首页热门企业", notes = "查询首页热门企业")
    public CommonResult findHotEnt(@RequestParam(value = "length") Integer length) {
        List<EntHotVo> result =null;
        try{
            result = enterpriseService.findHotEnt(length);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("查询失败！");
        }
        return CommonResult.success(result);
    }

    //公司栏目条件查询
    @PostMapping("/findRecByFunnel")
    @ApiOperation(value = "公司栏目条件查询", notes = "公司栏目条件查询")
    public CommonResult<PageInfo<EntNavVo>> findEntByFunnel(@RequestBody FunnelEntDto funnelEntDto){
        PageInfo<EntNavVo> result =null;
        try{
            result = enterpriseService.findEntByFunnel(funnelEntDto);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("查询失败！");
        }
        return CommonResult.success(result);
    }

    //栏目下热门企业
    @GetMapping("/findRecHotEnt")
    @ApiOperation(value = "栏目下热门企业", notes = "栏目下热门企业")
    public CommonResult findRecHotEnt(@RequestParam Integer length) {
        List<String> result =null;
        try{
            result = enterpriseService.findRecHotEnt(length);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("查询失败！");
        }
        return CommonResult.success(result);
    }

    //公司详情页
    @GetMapping("/entDetail")
    @ApiOperation(value = "公司详情页", notes = "公司详情页")
    public CommonResult<EntDetailVo> entDetail(@RequestParam Integer id) {
        EntDetailVo result = null;
        try{
            result = enterpriseService.entDetail(id);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("查询失败！");
        }
        return CommonResult.success(result);
    }

    //获取全部企业信息
    @GetMapping("/all")
    @ApiOperation(value = "获取全部企业信息",notes = "获取全部企业信息")
    public CommonResult<List<EnterprisePo>> findAll(){
        List<EnterprisePo> list = null;
        try{
            list = enterpriseService.findAll();
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("查询全部企业数据失败");
        }
        return CommonResult.success(list);
    }

    //获取全部企业信息
    @GetMapping("/findById")
    @ApiOperation(value = "获取单个企业信息",notes = "获取单个企业信息")
    public CommonResult<EnterprisePo> findById(@RequestParam Integer id){
        EnterprisePo result = null;
        try{
            result = enterpriseService.findById(id);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("查询全部企业数据失败");
        }
        return CommonResult.success(result);
    }

    //获取全部企业信息（分页）
    @PostMapping("/allPageInfo")
    @ApiOperation(value = "获取全部企业信息（分页）",notes = "获取全部企业信息（分页）")
    public CommonResult<PageInfo<EnterprisePo>> findAllPageInfo(@RequestBody PageDto pageDto){
        PageInfo<EnterprisePo> list = null;
        try{
            list = enterpriseService.findAllPageInfo(pageDto);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("查询全部企业数据失败");
        }
        return CommonResult.success(list);
    }

    //测试Service接口
    @PostMapping("/insert")
    @ApiOperation(value = "添加企业", notes = "添加企业")
    public CommonResult insert(@RequestBody EnterprisePo enterprisePo) {
        int result = 0;
        try{
            result = enterpriseService.insert(enterprisePo);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("添加失败！");
        }
        return CommonResult.success("添加"+result+"条数据成功！");
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改企业数据", notes = "修改企业数据")
    public CommonResult update(@RequestBody EnterprisePo enterprisePo) {
        int result = 0;
        try{
            result = enterpriseService.update(enterprisePo);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("修改失败！");
        }
        return CommonResult.success("修改"+result+"条数据成功！");
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除企业数据", notes = "删除企业数据")
    public CommonResult delete(@RequestParam Integer id) {
        int result = 0;
        try{
            result = enterpriseService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.success("删除失败！");
        }
        return CommonResult.success("删除"+result+"条数据成功！");
    }

    @DeleteMapping("/deleteList")
    @ApiOperation(value = "批量删除企业数据", notes = "批量删除企业数据")
    public CommonResult deleteList(String  list) {
        int result = 0;
        try{
            result = enterpriseService.deleteList(list);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.success("删除失败！");
        }
        return CommonResult.success("删除"+result+"条数据成功！");
    }
}
